package m3f.interact;

import m3f.data.MultimodalCachedLoader;
import m3f.data.MultimodalDataSet;
import m3f.factorization.OnlineNSE;
import m3f.io.DenseMatrixWriter;

public class ONSE {
	public static void factors(String[] args){
		// Data sources
		if (args.length < 12){
			System.out.println("Parameters: \n" +
					"\t1  Training matrix of visual features\n" +
					"\t2  Training matrix of textual \n" +
					"\t3  Test matrix of visual \n" +
					"\t4  Test matrix of textual \n" +
					"\t5  Latent regularizer (lambda) \n" +
					"\t6  Prediction regularizer (beta) \n" +
					"\t7 Initial step size (gamma0) \n" +
					"\t8 Minibatch size \n" +
					"\t9 Epoch number \n" +
					"\t10 Test output file\n" +
					"\t11 S output file\n" +
					"\t12 Skip Error\n");
			System.exit(0);
		}
		String vFile = args[1];
		String tFile = args[2];
		String vtFile = args[3];
		String ttFile = args[4];
		
		// Model parameters
		double lambda = Double.valueOf(args[5]);
		double beta = Double.valueOf(args[6]);
		double gamma0 = Double.valueOf(args[7]);
		int minibatch = Integer.valueOf(args[8]);
		int epochs = Integer.valueOf(args[9]);
		String outputTest = args[10];
		String outputS = args[11];
		boolean skipError = Boolean.parseBoolean(args[12]);

		// Run the algorithm
		long start = System.currentTimeMillis();
		MultimodalCachedLoader set = new MultimodalCachedLoader(vFile, tFile, true);
		OnlineNSE factorization = null;
		
		factorization = new OnlineNSE(set);
		System.out.println("Loading training data: " + (System.currentTimeMillis()-start) + "ms");
		start = System.currentTimeMillis();
		factorization.run(lambda, gamma0, epochs, minibatch, skipError);

		start = System.currentTimeMillis();
		MultimodalDataSet test = new MultimodalDataSet(vtFile, ttFile, set.visualFeatures(), set.textFeatures(), true);
		DenseMatrixWriter.writeMatrix(factorization.getS(), outputS, true);
		DenseMatrixWriter.writeMatrix(factorization.projectVisualData(test, beta), outputTest, true);
		System.out.println("Processing test data: " + (System.currentTimeMillis()-start) + "ms");
	}
}
